package com.luban;

import com.luban.service.UserInterface;
import com.luban.service.UserService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author leimingchao
 * create 2025-04-27 19:45
 */
public class JdkProxyTest {

	public static void main(String[] args) {

		UserService userService = new UserService();

		UserInterface proxyInterface = (UserInterface) Proxy.newProxyInstance(UserService.class.getClassLoader(), UserService.class.getInterfaces(), new InvocationHandler() {

			/**
			 * @param proxy 代理对象
			 * @param method 代理对象执行的方法
			 * @param args 方法参数
			 * @return
			 * @throws Throwable
			 */
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println("代理逻辑");

				Object result = method.invoke(userService, args);

				return result;
			}
		});

		proxyInterface.test();
	}
}
